package com.hzz.framework.config;

import com.alibaba.druid.pool.DruidDataSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.annotation.Order;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Properties;

/**
 * MyBatis基础配置
 */
@Configuration
@AutoConfigureAfter(AppConfig.class)
@EnableTransactionManagement
@Slf4j
@Order(-999)
public class JdbcConfig {
    @Autowired
    DataSourceProperties dataSourceProperties;

    @Bean(name = "dataSource", initMethod = "init", destroyMethod = "close")
    @Order(1)
    @Primary
    public DataSource writeDataSource() throws SQLException {
        log.info("注入 druid！！！");
        DruidDataSource dataSource = new DruidDataSource();
        String url = dataSourceProperties.getUrl();
        if (url != null) {
            dataSource.setUrl(url);
        }
        String username = dataSourceProperties.getUsername();
        if (username != null) {
            dataSource.setUsername(AppConfig.decrypt(username));//用户名
        }
        String password = dataSourceProperties.getPassword();
        if (password != null) {
            dataSource.setPassword(AppConfig.decrypt(password));//密码
        }
        String driverClassName = dataSourceProperties.getDriverClassName();
        if (driverClassName != null) {
            dataSource.setDriverClassName(driverClassName);
        }
        Integer initialSize = dataSourceProperties.getInitialSize();
        if (initialSize != null) {
            dataSource.setInitialSize(initialSize);
        }
        Integer minIdle = dataSourceProperties.getMinIdle();
        if (minIdle != null) {
            dataSource.setMinIdle(minIdle);
        }
        Integer maxActive = dataSourceProperties.getMaxActive();
        if (maxActive != null) {
            dataSource.setMaxActive(maxActive);
        }
        Integer maxWait = dataSourceProperties.getMaxWait();
        if (maxWait != null) {
            dataSource.setMaxWait(maxWait);
        }
        Integer timeBetweenEvictionRunsMillis = dataSourceProperties.getTimeBetweenEvictionRunsMillis();
        if (timeBetweenEvictionRunsMillis != null) {
            dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        }
        Integer minEvictableIdleTimeMillis = dataSourceProperties.getMinEvictableIdleTimeMillis();
        if (minEvictableIdleTimeMillis != null) {
            dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        }
        String validationQuery = dataSourceProperties.getValidationQuery();
        if (validationQuery != null) {
            dataSource.setValidationQuery(validationQuery);
        }
        Boolean testOnBorrow = dataSourceProperties.getTestOnBorrow();
        if (testOnBorrow != null) {
            dataSource.setTestOnBorrow(testOnBorrow);
        }
        Boolean testWhileIdle = dataSourceProperties.getTestWhileIdle();
        if (testWhileIdle != null) {
            dataSource.setTestWhileIdle(testWhileIdle);
        }
        Boolean testOnReturn = dataSourceProperties.getTestOnReturn();
        if (testOnReturn != null) {
            dataSource.setTestOnReturn(testOnReturn);
        }
        Boolean poolPreparedStatements = dataSourceProperties.getPoolPreparedStatements();
        if (poolPreparedStatements != null) {
            dataSource.setPoolPreparedStatements(poolPreparedStatements);
        }
        Integer maxPoolPreparedStatementPerConnectionSize = dataSourceProperties.getMaxPoolPreparedStatementPerConnectionSize();
        if (maxPoolPreparedStatementPerConnectionSize != null) {
            dataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
        }
        //配置监控统计拦截的filters，去掉后监控界面sql无法统计，'wall'用于防火墙
        String filters = dataSourceProperties.getFilters();
        if (filters != null) {
            dataSource.setFilters(filters);
        }

        String connectionProperties = dataSourceProperties.getConnectionProperties();
        if (connectionProperties != null) {
            String[] ps = connectionProperties.split(";");
            Properties props = new Properties();
            for (String line : ps) {
                String[] ones = line.split(":");
                props.put(ones[0].trim(), ones[1].trim());
            }
            dataSource.setConnectProperties(props);
        }

        Boolean useGlobalDataSourceStat = dataSourceProperties.getUseGlobalDataSourceStat();
        if (useGlobalDataSourceStat != null) {
            dataSource.setUseGlobalDataSourceStat(useGlobalDataSourceStat);
        }
        return dataSource;
    }
}